<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>layui</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

	<link href="../../static/font-awesome-4.7.0/css/font-awesome.min.css" rel="stylesheet">
	<link rel="stylesheet" href="../../static/vui/css/vui.css">

	<script type="text/javascript" src="../../static/js/jquery-1.9.1.min.js"></script>
	<script type="text/javascript" src="../../static/layer/layer.js"></script>
	<script type="text/javascript" src="../../static/laydate/laydate.js"></script>
	<script type="text/javascript" src="../../static/vui/js/vui.js"></script>
	<script type="text/javascript" src="../../static/vui/js/datagrid.js"></script>
	<script type="text/javascript" src="../../static/js/common.js"></script>
</head>
<body>
<table class="table-top" id="formArea">
	<tr>
		<td>
			<label class="label">单据日期</label>
		</td>
		<td>
			<input type="text" n="date" class="input search-date" name="dateStart" autocomplete="off"/>&nbsp;-&nbsp;
			<input type="text" n="date" class="input search-date" name="dateEnd" autocomplete="off"/>
		</td>

		<td>
			<label>单据编号</label>
		</td>
		<td>
			<input type="text" class="input input-query" name="code" autocomplete="off"/>
		</td>

		<td>
			<button class="button" id="query"><span>查询</span></button>
		</td>
	</tr>
</table>
<div id="table"></div>
<script>

    var params = app.getRequestParams();
    function getOpenFrame(){
        if( params.index == -1 || params.index == 'undefined' ){
            return parent;
        }else{
            return parent.frames[0];
        }
    }

    $(function(){

        $('#query').click(function () {
            var queryParams = app.serializeForm();
            queryParams.relationId = params.relationId;
            queryParams.relationType = params.relationType;
            queryParams.code = deletePrefix(queryParams.code);
            $('#table').datagrid("reload",queryParams);
        });

        var nowDate = app.time2str(new Date()),startDate = new Date(nowDate.substr(0,8)+'01');

        var dateStart = laydate.render({
            elem: 'input[name="dateStart"]',value:startDate,max:nowDate,
            done: function(value,date){
                if(value){
                    date.month=date.month-1;
                    dateEnd.config.min = date;
                }else{
                    dateEnd.config.min=dateStart.config.min;
                }
            }
        });
        var dateEnd = laydate.render({
            elem: 'input[name="dateEnd"]',value:nowDate,min:nowDate.substr(0,8)+'01',
            done: function(value,date){
                if(value){
                    date.month=date.month-1;
                    dateStart.config.max = date;
                }else{
                    dateStart.config.max=dateEnd.config.max;
                }
            }
        });

        $('#table').datagrid({
            url:app.BASE_API_URL+'verificate/verifiPage',checkbox:true,
			queryParams:params,
            columns:[
                {field:'code',title:'单据编号',width:100,formatter:function (row) {
                        return '<a href="javascript:void(0);" onclick="selectRow(this)" class="link">'+codeFormat(row)+'</a>';
                    }},
                {field:'billType',title:'业务类别',width:80,formatter:billTypeFormat},
                {field:'date',title:'单据日期',width:100,n:'date'},
                {field:'price',title:'单据金额',width:100,n:'money'},
                {field:'verificatedMoney',title:'已核销金额',width:100,n:'money'},
                {field:'xxx',title:'未核销金额',width:100,n:'money',fixed:true,formatter:function (row) {
						return app.div(app.sub(row.price,row.verificatedMoney),app.DIV);
                    }}
            ],
			loadSuccess:function () {
                var parentIds = getParentIds();
                $('#table .vui-table-row').each(function () {
                    var row = $(this).data();
                    for( var i =0 ; i < parentIds.length ; i++ ){
                        if( row.id == parentIds[i] ){
                            $(this).find('.vui-table-cell input[type="checkbox"]').prop("checked",true);
						}
					}
                });
            }
        });

    });

    function getParentIds(type) {
        var doc = getOpenFrame().document;
        var parentIds = [];
        $('#fixtable'+type+' .ctx-right .vui-table-row',doc).each(function () {
            var tid = $(this).attr('tid');
            if( tid != null && tid != undefined && $.trim(tid).length > 0 )
                parentIds.push(tid);
        });
        return parentIds;
    }

    function writeRow(row,type) {
        var parentIds = getParentIds(type);
        for( var i =0 ; i < parentIds.length ; i++ ){
            if( row.id == parentIds[i] ){
                return;
            }
        }

        var doc = getOpenFrame();
        doc.$('#fixtable'+type+' .ctx-right .vui-table-row').each(function () {
            var orderTypeEle = $(this).find('.vui-table-cell[field="orderType"]',doc);
            if( $.trim(orderTypeEle.html()).length == 0 ){
                $(this).attr('tid',row.id);
                doc.$(this).data(row);
                orderTypeEle.html(billTypeFormat(row));
                $(this).find('.vui-table-cell[field="date"]',doc).html(app.time2str(row.date));
                $(this).find('.vui-table-cell[field="code"]',doc).html(codeFormat(row));
                $(this).find('.vui-table-cell[field="orderMoney"]',doc).html(app.div(row.price,app.DIV));
                $(this).find('.vui-table-cell[field="verificateMoney"]',doc).html(app.div(row.verificatedMoney,app.DIV));
                $(this).find('.vui-table-cell[field="xxx"]',doc).html(app.div(app.sub(row.price,row.verificatedMoney),app.DIV));
                $(this).find('.vui-table-cell[field="currVerificateMoney"]',doc).html('');
                return false;
			}
        });

    }

    function selectRow(_this) {
        var row = $(_this).parent().parent().data();
        writeRow(row,params.btnType);
        app.closeLastWindow();
    }

    app.getOkBtn().click(function(){
        var rows = $('#table').datagrid('getSelections');
        for(var i=0 ; i<rows.length ; i++){
            writeRow(rows[i],params.btnType);
		}
		app.closeLastWindow();
	});

    function codeFormat(row) {
        if( row.orderType ){
            if( row.orderType == 22 ){
                return 'SH'+row.code;
            }else if( row.orderType == 4 ){
                return 'CT'+row.code;
            }else if( row.orderType == 23 ){
                return 'XT'+row.code;
            }else if( row.orderType == 3 ){
                return 'CG'+row.code;
            }
		}else{
            if( row.billType == 1 ){
                return 'SK'+row.code;
			}else if( row.billType == 2 ){
                return 'FK'+row.code;
			}
		}
    }

    function billTypeFormat(row) {
        if( row.orderType ){
            if( row.orderType == 22 ){
                return '销售单';
            }else if( row.orderType == 4 ){
                return '采购退货单';
            }else if( row.orderType == 23 ){
                return '销售退货单';
            }else if( row.orderType == 3 ){
                return '采购单';
            }
		}else{
            if( row.billType == 1 ){
                return '收款单';
            }else if( row.billType == 2 ){
                return '付款单';
            }
		}

    }

</script>
</body>
</html>